<?php
/**
 * User: hjd-yao
 * Date: 2019/7/24
 * Time: 10:37
 */

namespace Shop\Http\Controllers\Traits;


use App\Helpers\SelfResponse;
use App\Models\Shop\UserAddressModel;
use Illuminate\Http\Request;

trait AddressTrait
{
    public function delete($addr_id)
    {
        $user_id = \request()->input('user_id');
        $info = UserAddressModel::find($addr_id);
        if(!$info||$info->user_id!=$user_id){
            return SelfResponse::error('删除的地址不存在');
        }
        if(UserAddressModel::where('id',$addr_id)->where('user_id',$user_id)->delete()){
            return SelfResponse::success('地址删除成功','f_Wap_Address_myAddress');
        }
        return SelfResponse::error('地址删除失败');
    }

    public function setDefault(Request $request,$addr_id)
    {
        $user_id = $request->input('user_id');
        $info = UserAddressModel::find($addr_id);
        if(!$info||$info->user_id!=$user_id){
            return SelfResponse::error('操作的地址不存在');
        }

        if($info->is_default ==1){
            return SelfResponse::error('该地址已是默认地址,无须操作');
        }

        UserAddressModel::where('user_id',$user_id)->where('id',$addr_id)->update(['is_default'=>1]);
        UserAddressModel::where('user_id',$user_id)->where('id','!=',$addr_id)->update(['is_default'=>0]);

        return SelfResponse::success('设置成功','f_Wap_Address_myAddress');
    }
}